package academic.emailClient.control.listeners;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import academic.emailClient.model.EmailClientModel;
import academic.emailClient.view.EmailClientViewInterface;
import academic.emailClient.view.VelocityTemplate;

public class ManageTemplatesComboBoxChangedListener implements ItemListener {
	EmailClientViewInterface view;
	EmailClientModel model;
	VelocityTemplate emailDemo;
	
	/**
	 * TemplatesChangedListener Constructor
	 * @throws Exception 
	 */
	
	public ManageTemplatesComboBoxChangedListener(EmailClientViewInterface view, EmailClientModel model) throws Exception{
		this.model = model;
		this.view = view;
		emailDemo = new VelocityTemplate();
	}
	
	/**
	 * Handle Manage Templates ComboBox Changed event
	 */
	@Override
	public void itemStateChanged(ItemEvent e) {
		 if (e.getStateChange() == ItemEvent.SELECTED) {
			 	if(e.getItem().toString() != "Select Template"){			 	
				try {
					String string = e.getItem().toString();
					String content = emailDemo.getTemplateString(string);
					view.updateManageTemplateTextAreaField(content);
				} catch (ResourceNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ParseErrorException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			 }
			
		 }
	}

}
